package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.pojo.InterventionSetmeal;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Base64;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setMeal")
public class SetMealController {
    @Reference
    private SetMealService setMealService;
    @RequestMapping("/getSetmeal")
    public Result getSetmeal(){
        try {
            List<Setmeal> list = setMealService.getSetmeal();
            return new Result(true,MessageConstant.GET_SETMEAL_LIST_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }
    @RequestMapping("/findAllById")
    public Result findAllById(Integer id){
        try {
            Setmeal setmeal = setMealService.findAllById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/getSetmealByPhone")
    public Result getSetmealByPhone(HttpServletRequest request){
        try {
            //回显用户信息
            String telephone = null; //创建一个变量,用于存储从cookie中的手机号;
            //1.登录后从cookie中获取手机号
            Cookie[] cookies = request.getCookies();
            telephone = null;
            //2.先判断cookie集合是否存在
            if (cookies != null) { //存在
                //2.1遍历cookie集合
                for (Cookie c : cookies) {
                    //2.1.1判断此cookie是否为存手机号的键,并给telephone赋值
                    if ("login_member_telephone".equals(c.getName())) {
                        //base64编码后的cookie值
                        String value= c.getValue();
                        //获取base64解码器
                        Base64.Decoder decoder = Base64.getDecoder();
                        //解码
                        byte[] bytes= decoder.decode(value);
                        String phoneNumber=new String(bytes);
                        telephone = phoneNumber;
                    }
                }
            }
            List<InterventionSetmeal> interventionSetmeal = setMealService.getSetmealByPhone(telephone);
            /*System.out.println(interventionSetmeal+"------------------------------");*/
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,interventionSetmeal);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    //通过日期查询套餐信息
    @RequestMapping("/findByDate")
    public Result findByDate(HttpServletRequest request, @RequestBody Map map) {
        //回显用户信息
        String telephone = null; //创建一个变量,用于存储从cookie中的手机号;
        //1.登录后从cookie中获取手机号
        Cookie[] cookies = request.getCookies();
        telephone = null;
        //2.先判断cookie集合是否存在
        if (cookies != null) { //存在
            //2.1遍历cookie集合
            for (Cookie c : cookies) {
                //2.1.1判断此cookie是否为存手机号的键,并给telephone赋值
                if ("login_member_telephone".equals(c.getName())) {
                    //base64编码后的cookie值
                    String value= c.getValue();
                    //获取base64解码器
                   Base64.Decoder decoder = Base64.getDecoder();
                   //解码
                   byte[] bytes= decoder.decode(value);
                    String phoneNumber=new String(bytes);
                    telephone = phoneNumber;
                }
            }
        }
        System.out.println("测试是否可以获取前台发送的日期和cookie中的手机号:" + map.size() + ":::" + telephone);
        try {
            List<InterventionSetmeal> interventionSetmeal = setMealService.findByDate(map, telephone);

            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, interventionSetmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/find")
    public Result find(Integer oid,HttpServletRequest request){
        try {
           /* //回显用户信息
            String telephone = null; //创建一个变量,用于存储从cookie中的手机号;
            //1.登录后从cookie中获取手机号
            Cookie[] cookies = request.getCookies();
            telephone = null;
            //2.先判断cookie集合是否存在
            if (cookies != null) { //存在
                //2.1遍历cookie集合
                for (Cookie c : cookies) {
                    //2.1.1判断此cookie是否为存手机号的键,并给telephone赋值
                    if ("login_member_telephone".equals(c.getName())) {
                        //base64编码后的cookie值
                        String value= c.getValue();
                        //获取base64解码器
                        Base64.Decoder decoder = Base64.getDecoder();
                        //解码
                        byte[] bytes= decoder.decode(value);
                        String phoneNumber=new String(bytes);
                        telephone = phoneNumber;
                    }
                }
            }*/
            Map<String,Object> map = setMealService.findByOId(oid);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
}
